# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

_luaversions="5.1 5.2 5.3 5.4"

pkgname=lua-posix
_name=luaposix
pkgver=35.1
pkgrel=0
pkgdesc="POSIX library for Lua $_luaver"
url="https://github.com/luaposix/luaposix"
arch="all"
license="MIT"
makedepends="linux-headers lua-bit32"

for _i in $_luaversions; do
	makedepends="$makedepends lua$_i-dev lua$_i"
	subpackages="$subpackages lua$_i-posix:_posix_${_i/./_}"
done
source="luaposix-$pkgver.tar.gz::https://github.com/luaposix/luaposix/archive/v$pkgver.tar.gz"

builddir="$srcdir"/$_name-$pkgver

prepare() {
	default_prepare
	for _i in $_luaversions; do
		cp -r "$builddir" "$srcdir"/build-$_i
	done
}

build() {
	for _i in $_luaversions; do
		msg "build for Lua $_i"
		cd "$srcdir"/build-$_i
		lua$_i build-aux/luke
	done
}

package() {
	for _i in $_luaversions; do
		cd "$srcdir"/build-$_i
		lua$_i build-aux/luke install PREFIX="$pkgdir"/usr
	done
}

_split() {
	depends=
	local d= _ver=$1 _f=$2
	if [ "$_ver" != "5.2" ]; then
		# use versioned dep to work around
		# http://bugs.alpinelinux.org/issues/4063
		depends="lua$_ver-bit32<26"
	fi
	for d in usr/lib/lua usr/share/lua; do
		if [ -d "$pkgdir"/$d/$_ver ]; then
			mkdir -p "$subpkgdir"/$d/$_ver
			mv "$pkgdir"/$d/$_ver/$_f* "$subpkgdir"/$d/$_ver/
			rmdir "$pkgdir"/$d/$_ver 2>/dev/null || true
		fi
	done
}

_split_posix() {
	local _ver=$1
	pkgdesc="POSIX library for Lua $_ver"
	install_if="lua$_ver lua-posix=$pkgver-r$pkgrel"
	_split $_ver posix
}

for _i in $_luaversions; do
	eval "_posix_${_i/./_}() { _split_posix $_i; }"
done

sha512sums="
f2c36bf69c0d73b4a2c6ab0915f81473addce427c22e82e2d1c334016dec7fecb4365fc980fa6adb0de21ad1a60bc75b73f58579e8a423990ad2067c9ae37775  luaposix-35.1.tar.gz
"
